package com.example.knowledgeget1.KnowtCommon.Service;

import com.example.knowledgeget1.KnowtCommon.Entity.Course;
import com.example.knowledgeget1.KnowtCommon.Entity.Teach;
import com.example.knowledgeget1.KnowtCommon.Entity.Teacher;
import com.example.knowledgeget1.KnowtCommon.Repository.CourseRepository;
import com.example.knowledgeget1.KnowtCommon.Repository.StudyRepository;
import com.example.knowledgeget1.KnowtCommon.Repository.TeachRepository;
import com.example.knowledgeget1.KnowtCommon.Response.CoursesResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class CourseService {
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private StudyRepository studyRepository;
    @Autowired
    private TeachRepository teachRepository;
    @Autowired
    private TeachService teachService;

    // 根据学生ID获取课程列表
    public List<CoursesResponse> getCoursesByStudentId(Long stuId) {
        List<Long> courseIds = studyRepository.findCourseIdsByStudentId(stuId);
        List<Course> courses = courseRepository.findByCourseIdIn(courseIds);
        List<CoursesResponse> coursesResponses = new ArrayList<>();

        for (Course course : courses) {
            Teach teach = teachRepository.findTeaIdByCourseId(course.getCourseId());
            Teacher teacher = teachService.getTeacherNameById(teach.getTeaId());

            CoursesResponse coursesResponse = new CoursesResponse();
            coursesResponse.setCourseName(course.getCourseName());
            coursesResponse.setCourseSchool(course.getCourseSchool());
            coursesResponse.setCoursePhoto(course.getCoursePhoto());
            coursesResponse.setCourseDescription(course.getCourseDescription());
            coursesResponse.setCourseId(String.valueOf(course.getCourseId()));
            coursesResponse.setTeacherName(teacher.getTeaName());

            coursesResponses.add(coursesResponse);
        }

        return coursesResponses;
    }

    // 根据教师ID获取课程列表
    public List<CoursesResponse> getCoursesByTeacherId(Long teaId) {
        List<Long> courseIds = teachRepository.findCourseIdsByTeacherId(teaId);
        List<Course> courses = courseRepository.findByCourseIdIn(courseIds);
        List<CoursesResponse> coursesResponses = new ArrayList<>();

        for (Course course : courses) {
            Teacher teacher = teachService.getTeacherNameById(teaId);
            CoursesResponse coursesResponse = new CoursesResponse();
            coursesResponse.setCourseName(course.getCourseName());
            coursesResponse.setCourseSchool(course.getCourseSchool());
            coursesResponse.setCoursePhoto(course.getCoursePhoto());
            coursesResponse.setCourseDescription(course.getCourseDescription());
            coursesResponse.setCourseId(String.valueOf(course.getCourseId()));
            coursesResponse.setTeacherName(teacher.getTeaName());

            coursesResponses.add(coursesResponse);
        }
        return coursesResponses;
    }

    // 创建新课程
    public Course createCourse(Course course) {
        return courseRepository.save(course);
    }

    // 获取所有课程
    public List<Course> getAllCourses() {
        return courseRepository.findAll();
    }

    // 通过ID获取单个课程
    public Course getCourseById(Long id) {
        Optional<Course> course = courseRepository.findById(id);
        return course.orElse(null);
    }

    // 更新课程信息
    public Course updateCourse(Long id, Course courseDetails) {
        Course course = getCourseById(id);
        if (course != null) {
            course.setCourseName(courseDetails.getCourseName());
            // 更新其他字段...
            courseRepository.save(course);
        }
        return course;
    }

    // 删除课程
    public void deleteCourse(Long id) {
        courseRepository.deleteById(id);
    }
}